#include "head.h"
//1 出现粘包问题
//int transfile(int netfd){
//    send(netfd,"file1",5,0);
//    int fd = open("file1",O_RDWR);
//    char buf[1024] = {0};
//    ssize_t sret = read(fd,buf,sizeof(buf));
//    send(netfd,buf,sret,0);
//    return 0;
//}
//2 使用小火车传小文件
//int transfile(int netfd){
//    train_t train;
//    train.length = 5;
//    memcpy(train.data,"file1",5);
//    send(netfd,&train,sizeof(train.length)+train.length,0);
//    int fd = open("file1",O_RDWR);
//    ssize_t sret = read(fd,train.data,sizeof(train.data));
//    train.length = sret;
//    send(netfd,&train,sizeof(train.length)+train.length,0);
//    return 0;
//}
//3.1 使用小火车传大文件
//int transfile(int netfd){
//    train_t train;
//    train.length = 5;
//    memcpy(train.data,"file1",5);
//    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
//    int fd = open("file1",O_RDWR);
//    struct stat statbuf;
//    fstat(fd,&statbuf);
//    train.length = sizeof(statbuf.st_size);
//    memcpy(train.data,&statbuf.st_size, train.length);
//    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
//    while(1){
//        ssize_t sret = read(fd,train.data,sizeof(train.data));
//        if(sret == 0){
//            break;
//        }
//        train.length = sret;
//        send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
//    }
//    train.length = 0;
//    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
//    return 0;
//}
//3.2 使用mmap
//int transfile(int netfd){
//    train_t train;
//    train.length = 5;
//    memcpy(train.data,"file1",5);
//    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
//    int fd = open("file1",O_RDWR);
//    struct stat statbuf;
//    fstat(fd,&statbuf);
//    train.length = sizeof(statbuf.st_size);
//    memcpy(train.data,&statbuf.st_size, train.length);
//    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
//    // 先mmap
//    char *p = (char *)mmap(NULL,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
//    ERROR_CHECK(p,MAP_FAILED,"mmap");
//    // 发送文件内容
//    off_t totalsize = 0;
//    while(totalsize < statbuf.st_size){
//        if(statbuf.st_size - totalsize > (long )sizeof(train.data)){
//            train.length = sizeof(train.data);
//        }
//        else{
//            train.length = statbuf.st_size - totalsize;
//        }
//        send(netfd,&train.length,sizeof(train.length),0);
//        send(netfd,p+totalsize,train.length,0);//零拷贝
//        totalsize += train.length;
//    }
//    train.length = 0;
//    send(netfd,&train.length,sizeof(train.length),0);
//    munmap(p,statbuf.st_size);
//    return 0;
//}
//4 大火车
int transfile(int netfd){
    train_t train;
    train.length = 5;
    memcpy(train.data,"file1",5);
    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    int fd = open("file1",O_RDWR);
    struct stat statbuf;
    fstat(fd,&statbuf);
    train.length = sizeof(statbuf.st_size);
    memcpy(train.data,&statbuf.st_size, train.length);
    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    //// 先mmap
    //char *p = (char *)mmap(NULL,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    //ERROR_CHECK(p,MAP_FAILED,"mmap");
    //// 发送文件内容
    //send(netfd,p,statbuf.st_size,MSG_NOSIGNAL);//大火车 零拷贝
    //munmap(p,statbuf.st_size);
    sendfile(netfd,fd,NULL,statbuf.st_size);
    return 0;
}
